package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLLoaderDataFormat;
	import flash.utils.ByteArray;
	import nochump.util.zip.ZipEntry;
	import nochump.util.zip.ZipFile;
	
	/**
	 * ...
	 * @author db0@qq.com
	 */
	public class XlsxParser extends Sprite
	{
		private var bytes:ByteArray = null;
		private var zipFile:ZipFile = null;
		private var xml:XML = null;
		private var paragraphs:Array = null;
		
		public function XlsxParser(bytes:ByteArray = null)
		{
			super();
			if (bytes)
			{
				this.bytes = bytes;
				unzip();
			}
			else
			{
				SwfLoader.loadData("xlsx.xlsx", loaded, URLLoaderDataFormat.BINARY);
			}
		}
		
		private function loaded(e:Event):void
		{
			if (e && e.type == Event.COMPLETE)
			{
				bytes = e.target.data as ByteArray;
			}
			unzip();
		}
		
		private function unzip():void
		{
			if (bytes == null)
				return;
			if (zipFile == null)
			{
				zipFile = new ZipFile(bytes);
				trace(zipFile.entries.join("\n"));
				trace("===========================");
			}
			var entry:ZipEntry = zipFile.getEntry("xl/sharedStrings.xml");
			
			if (entry)
			{
				var data:ByteArray = zipFile.getInput(entry);
				if (data)
				{
					xml = XML(data);
					var ns:Namespace = xml.namespace();
					trace("namespace:", ns);
					var ls:Array = new Array();
					for each (var t:XML in xml.ns::si)
					{
						ls.push(t.ns::t);
							//trace(t.ns::t);
					}
					for each (var _entry:ZipEntry in zipFile.entries)
					{
						if (_entry.name.match(/^xl\/worksheets\/[^\/]+.xml$/))
						{
							//var _entry:ZipEntry = zipFile.getEntry("xl/worksheets/sheet1.xml");
							var sheetStream:ByteArray = zipFile.getInput(_entry);
							var sheetXML:XML = XML(String(sheetStream));
							//var ns:Namespace = sheetXML.namespace();
							//trace(sheetXML.ns::sheetData.children().length());
							if (paragraphs == null)
								paragraphs = new Array();
							paragraphs[paragraphs.length] = "\r\n----------- "+_entry.name.replace(/^xl\/worksheets\/([^\/]+).xml$/, "$1") + " ---------";
							var numRow:int = paragraphs.length;
							for each (var row:XML in sheetXML.ns::sheetData.children())
							{
								paragraphs[numRow] = "";
								for each (var c:XML in row.children())
								{
									if (c.@t == "s")
									{
										paragraphs[numRow] += String(ls[int(c.ns::v)]).replace(/[\r\n]/gm, "") + "\t";
											//trace(c.v, ls[int(c.ns::v)]);
									}
									else if (String(c.ns::v).length > 0)
									{
										switch (int(c.@s))
										{
										case 1: 
											paragraphs[numRow] += String(c.ns::v).replace(/[\r\n]/gm, "") + "\t";
											break;
										case 2: 
											paragraphs[numRow] += toData(String(c.ns::v).replace(/[\r\n]/gm, "")) + "\t";
											break;
										case 3: 
											paragraphs[numRow] += toData(String(c.ns::v).replace(/[\r\n]/gm, "")) + "\t";
											break;
										}
											//trace(c.ns::v);
									}
									else
									{
										//trace("-");
									}
								}
								numRow++;
							}
							paragraphs[numRow - 1] += "\r\n\r\n";
						}
					}
				}
			}
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function toData(s:String):String
		{
			//date from 1900.1.1 / 1904.1.2 - 9999.12.31
			//					1		-	2958465/2957003
			if (Number(s) != 0)
			{
				var i:Number = (Number(s) - 2) * 3600000 * 24;
				var date:Date = new Date(1900, 0, 1);
				var data2:Date = new Date(Number(date) + i);
				if (s.search(/\./) >= 0)
					return data2.fullYear + "/" + (data2.month + 1) + "/" + data2.date + "/" + data2.getHours() + ":" + data2.getMinutes() + ":" + data2.getSeconds();
				else
					return data2.fullYear + "/" + (data2.month + 1) + "/" + data2.date + "";
			}
			return s;
		
		}
		
		private function init(e:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			if (paragraphs)
				addChild(new MyText(3, 10, stage.stageWidth - 6, stage.stageHeight - 10, String(paragraphs.join("\n"))));
			else
				addChild(new MyText(3, 10, stage.stageWidth - 6, stage.stageHeight - 10, String("解析文件出现问题")));
		}
	}
}